//package com.mediacloud.app.newsmodule.view
//
//import android.content.BroadcastReceiver
//import android.content.Context
//import android.content.Intent
//import android.content.IntentFilter
//import android.util.Log
//import android.view.View
//import androidx.databinding.DataBindingUtil
//import androidx.lifecycle.LifecycleOwner
//import androidx.lifecycle.Observer
//import com.scwang.smartrefresh.layout.api.RefreshLayout
//import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
//import com.mediacloud.app.model.news.ArticleListData
//import com.mediacloud.app.model.news.CatalogItem
//import com.mediacloud.app.newsmodule.R
//import com.mediacloud.app.newsmodule.broadcast.AudioLiveBroadcast
//import com.mediacloud.app.newsmodule.broadcast.AudioVodBroadcast
//import com.mediacloud.app.newsmodule.databinding.TxqcAudioliveNavlayoutBinding
//import com.mediacloud.app.newsmodule.fragment.navlive.TYPE_GET_AUDIO_LIVE
//import com.mediacloud.app.newsmodule.fragment.navlive.TxqcLiveNavVM
//import com.mediacloud.app.newsmodule.utils.*
//import com.mediacloud.app.reslib.databinding.LibRequestLoadingLayoutBinding
//
//class TxqcLiveNavRadioControl(lifeOwner: LifecycleOwner, var binding: TxqcAudioliveNavlayoutBinding, private var txqcLiveNavVm: TxqcLiveNavVM, var catalogId: String?)
//    : Observer<ArticleListData?>, OnRefreshLoadMoreListener {
//    var adapter = TxqcLiveNavRadioLiveAdapter(binding.root.context, this::playRadio)
//    var callFlag = false
//    var loadingView: LibRequestLoadingLayoutBinding? = DataBindingUtil.bind(binding.root.findViewById(R.id.radioLoading))
//    private var pageIndex: Int = 1
//    private var reciver = AudioTimeUpdateReciver()
//    private var catalog = CatalogItem().apply {
//        catid = catalogId
//    }
//
//    init {
//        txqcLiveNavVm.radioLiveListObs.observe(lifeOwner, this)
//        binding.recyclerview.adapter = adapter
//        binding.refresh.setEnableRefresh(false)
//        binding.refresh.setOnRefreshLoadMoreListener(this)
//        adapter.setItemClickListener { index, _, _ ->
//            val intent = Intent()
//            NewsItemClickUtils.OpenItemNewsHandle(binding.root.context, adapter.getItem(index).type, adapter.getItem(index), catalog, intent)
//        }
//
//        var filter = IntentFilter()
//        filter.addAction(AudioLiveBroadcast.Audio_UpdateTime)
//        filter.addAction(AudioLiveBroadcast.AudioComplete)
//        filter.addAction(AudioVodBroadcast.Audio_UpdateTime)
//        filter.addAction(AudioVodBroadcast.AudioComplete)
//
//        binding.root.context.registerReceiver(reciver, filter)
//    }
//
//    fun destory() {
//        binding.root.context.unregisterReceiver(reciver)
//    }
//
//    inner class AudioTimeUpdateReciver : BroadcastReceiver() {
//        override fun onReceive(context: Context, intent: Intent) {
//            var currentPlayId = intent.getLongExtra(Intent.EXTRA_LOCUS_ID, -1)
//            if (currentPlayId == -1L)
//                return
//            Log.w("AppfacRadio", "当前播放的音频id:" + currentPlayId)
//            if (intent.action == AudioLiveBroadcast.Audio_UpdateTime || intent.action == AudioVodBroadcast.Audio_UpdateTime) {
//                var currentIndex = -1
//                adapter.data.forEachIndexed { index, articleItem ->
//                    if (articleItem.getId() == currentPlayId) {
//                        currentIndex = index
//                    }
//                }
//                if (currentIndex == -1)//相当于其他地方播放的音频  没有找到
//                {
//                    currentIndex = -1
//                    adapter.currentIndex = -1
//                    adapter.notifyDataSetChanged()
//                } else if (adapter.currentIndex != currentIndex) {
//                    adapter.currentIndex = currentIndex
//                    playIndex = currentIndex
//                    adapter.notifyDataSetChanged()
//                }
//            }
//
//        }
//
//    }
//
//    fun show() {
//        binding.root.visibility = View.VISIBLE
//        if (!callFlag) {
//            playIndex = -1
//            pageIndex = 1
//            loadData()
//        } else {
//            playIndex = -1
//            adapter.currentIndex = -1
////            binding.refresh.autoRefresh()
//            onRefresh(binding.refresh)
//        }
//    }
//
//    fun hide() {
//        binding.root.visibility = View.GONE
//        binding.root.context.stopPlayLiveAudio()
//    }
//
//    private fun loadData() {
//        callFlag = true
//        txqcLiveNavVm.getNavContentList("$catalogId", pageIndex, TYPE_GET_AUDIO_LIVE)
//    }
//
//    override fun onChanged(t: ArticleListData?) {
//        loadingView?.closeLoading()
//        if (t == null) {
//            if (adapter.itemCount == 0) {
//                loadingView?.showNetError()
//            }
//            return
//        }
//        t?.apply {
//            binding.refresh.finishRefresh()
//            binding.refresh.finishLoadMore()
//            binding.refresh.setEnableLoadMore(more)
//            binding.refresh.setNoMoreData(!more)
//            articleList?.let {
//                if (pageIndex == 1) {
//                    adapter.data.clear()
//                }
//                adapter.data.addAll(it)
//                if (pageIndex == 1 && adapter.currentIndex == -1) {
//                    adapter.currentIndex = 0
//                    playRadio(0)
//                }
//                adapter.notifyDataSetChanged()
//            }
//            if (adapter.itemCount == 0) {
//                loadingView?.showNoContent()
//            }
//        }
//    }
//
//    var playIndex = -1
//    var isPlay = false
//    var isPause = false
//    private fun playRadio(index: Int) {
//        if (index >= adapter.itemCount)
//            return
//        if (playIndex != index) {
//            isPlay = true
//            adapter.currentIndex = index
//            if (adapter.getItem(index).audio != null)
//                binding.root.context.playBackgroundLiveAudio(adapter.getItem(index), adapter.getItem(index).audio, catalog)
//        } else if (isPlay) {
//            isPlay = false
//            adapter.currentIndex = -1
//            binding.root.context.pauseLiveAudio()
//            isPause = true
//        } else if (isPause) {
//            isPause = false
//            isPlay = true
//            adapter.currentIndex = index
//            binding.root.context.resumeLiveAudio()
//        }
//        playIndex = index
//        adapter.notifyDataSetChanged()
//
//    }
//
//    override fun onLoadMore(refreshLayout: RefreshLayout) {
//        pageIndex += 1
//        loadData()
//    }
//
//    override fun onRefresh(refreshLayout: RefreshLayout) {
//        pageIndex = 1
//        loadData()
//    }
//}